<template>
  <div class="profession">
    <div class="header">
      <el-cascader
        v-model="majorNameKeyWords"
        size="large"
        placeholder="专业"
        :options="educateTypeOption"
        :props="props"
      />
			<el-input placeholder="专业课" size="large" v-model="majorExamName"></el-input>
      <el-select
        v-model="englishKeyWords"
        @change="englishChange"
        clearable
        class="m-2"
        placeholder="英语"
        size="large"
        style="width: 120px;"
      >
        <el-option
          v-for="item in englishOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="mathKeyWords"
        class="m-2"
        @change="mathChange"
        placeholder="数学"
        size="large"
        clearable
      >
        <el-option
          v-for="item in mathTypeOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-button
        type="primary"
        color="#ff7214"
        size="large"
        @click="getProfession"
        style="color: #fff"
      >查询</el-button>
    </div>
    <div class="list" v-loading="loading" element-loading-text="数据加载中...">
			<div v-loading="loading">
				<ListItem :data="listData"></ListItem>
        <Pagination
          v-if="listData.length> 0"
          :total="total"
          :pageNum="pageNum"
          :pageSize="pageSize"
          @currentChange="pageChange"
          @sizeChange="sizeChange"
        ></Pagination>
			</div>
    </div>
  </div>
</template>
<script setup>
import {ref, onMounted} from 'vue'
import {ElMessage} from 'element-plus'
import ListItem from '@/components/listItem/ListItem.vue'
import Pagination from '@/components/pagination/Pagination'
import {decrypt} from '@/utils/parseDate'
import {sameSubject, english,train, second,professionList} from '@/api/api'
import {useStore} from 'vuex'
// 获取store
const store = useStore()
// 加载
const loading = ref(false)
// 专业id
const majorNameKeyWords = ref('')
// 专业课名称
const majorExamName = ref('')
// 英语类型
const englishKeyWords = ref('')
const englishOptions = ref([])
// 考数学和不考数学
const mathKeyWords = ref('')
const mathTypeOptions = ref(['数学一', '数学二', '数学三','不考数学'])
// 查询结果
const listData = ref([])
// 培养类型:学硕 专硕
const educateTypeOption = ref([
  {
    label: '学术型硕士',
    value: 1
  },
  {
    label: '专业学位硕士',
    value: 2
  }
])
const props = {
  lazy: true,
  children: 'children',
  // leaf: 'isLeaf',
  lazyLoad: (node, resolve) => {
    const { level, pathValues } = node
    if (level === 1) {
      train().then(res=>{
        if(res.status === 0){
          let arr = []
					if(pathValues[0]==1){
						arr = res.result.subjectCategoryRsps.map(ele => ({
							label: ele.subjectCategoryName,
							leaf: level >= 3,
							value: ele.subjectCategoryCode
						}))
					} else if(pathValues[0]==2){
						arr = res.result.subjectCategoryRspsForZhuan.map(ele => ({
							label: ele.subjectCategoryName,
							leaf: level >= 3,
							value: ele.subjectCategoryCode
						}))
					}
          resolve(arr)
        } else {
          resolve(undefined)
        }
      })
      
    }
    if(level === 2){
      second({ subjectCategoryCode:pathValues[1] ,educateType: pathValues[0] }).then(res => {
        if(res.status ===0){
          const arr = res.result.map(ele=>({
            label:ele.major1Name,
            leaf: level>=3,
            value: ele.major1Code
          }))
          if(arr.length === 0){
            resolve(undefined)
          } else {
            resolve(arr)
          }
        }else {
          resolve(undefined)
        }
      })
    }
    if(level === 3){
      professionList({educateTypes: pathValues[0],subjectCategoryCode: pathValues[1],major1Code: pathValues[2]}).then(res=>{
        if(res.status === 0){
          const arr = res.result.data.map(ele=>({
            label:ele.majorName,
            value:ele.majorCode,
            leaf: level>=3
          }))
          resolve(arr)
        } else {
          resolve(undefined)
        }
      })
    }
    if(level === 4){
      resolve(undefined)
    }
  }
}
// 分页
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)
// 页码改变
function pageChange(page){
  pageNum.value = page
  if(majorNameKeyWords.value){
    getProfession()
  } else {
    getTestProfession()
  }
}
// 分页大小改变
function sizeChange(size){
  pageSize.value = size
  if(majorNameKeyWords.value){
    getProfession()
  } else {
    getTestProfession()
  }
}
// 同考试科目专业列表
async function getProfession() {
  try {
    if(!majorNameKeyWords.value){
      ElMessage.warning('请选择专业名称!')
      return
    }
		if(!majorExamName.value){
			ElMessage.warning('请输入专业课名称!')
      return
		}
    if(!englishKeyWords.value){
      ElMessage.warning('请选择英语类型!')
      return
    }
    loading.value = true
    listData.value = []
    const data = {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
			majorExamName: majorExamName.value,
      mathKeyWords: mathKeyWords.value,
      majorCode: majorNameKeyWords.value[majorNameKeyWords.value.length - 1],
      englishKeyWords: englishKeyWords.value
    }
    const result = await sameSubject(data)
    const res = JSON.parse(decrypt(result))
    if (res.status == 8) {
      store.commit("setTipsDialog", true);
    }
    if (res.status == 9) {
      store.commit("setNumberDialog", true);
    }
    if(res.status == 0 && res.result.data.length>0){
      res.result.data.forEach(ele=>{
        if(ele.schoolTag){
          ele.schoolTag= ele.schoolTag.split('|')
        }
        ele.checked = false
      })
      total.value = res.result.total
      listData.value = res.result.data
    } else {
			listData.value = []
		}
    loading.value = false
  } catch (error) {
    loading.value = false
    // throw new Error(error)
  }
}
// 测试数据
async function getTestProfession() {
  try {
    loading.value = true
    const data = {
      mathKeyWords: '数学三',
      majorCode: '020101',
			majorExamName: '经济学',
      englishKeyWords: '英语一',
      pageNum: pageNum.value,
      pageSize: pageSize.value
    }
    const result = await sameSubject(data)
    const res = JSON.parse(decrypt(result))
    if (res.status == 8) {
      store.commit("setTipsDialog", true);
    }
    if (res.status == 9) {
      store.commit("setNumberDialog", true);
    }
    if(res.status == 0){
      res.result.data.forEach(ele=>{
        if(ele.schoolTag){
          ele.schoolTag= ele.schoolTag.split('|')
        }
        ele.checked = false
      })
      total.value = res.result.total
      listData.value = res.result.data
    }
    loading.value = false
  } catch (error) {
    loading.value = false
    // throw new Error(error)
  }
}
// 切换数学类型
function mathChange(val) {
  mathKeyWords.value = val
}

// 切换英语类型
function englishChange(val) {
  englishKeyWords.value = val
}

// 获取英语类型
async function getEnglish() {
  try {
    const result = await english()
    if (result.status === 0) {
      englishOptions.value = JSON.parse(result.result)
    }
  } catch (error) {
    // throw new Error(error)
  }
}

onMounted(() => {
  getEnglish()
  getTestProfession()
})
</script>
<style scoped lang="less">
.profession {
  box-sizing: border-box;
  padding-top: 16px;
  height: 100%;
	width: 100%;
  .header {
    // margin-bottom: 24px;
    height: 54px;
    padding: 0 calc((100vw - 1200px)/2);
		min-width: 1200px;
    .el-select {
      margin-right: 10px;
    }
		.el-input{
			margin-right: 10px;
			width: 140px;
		}
    /deep/ .el-cascader{
      margin-right: 10px;
      width: 360px;
    }
  }
	.list{
		padding: 0 calc((100vw - 1200px)/2);
		min-width: 1200px;
		height: calc(100% - 54px);
		overflow: auto;
	}
}
</style>
